package com.kaikeba.homework.spring.util;

import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

/**
 * @author pengxinkui
 */
public class XmlUtil {
    private static XmlUtil instance = new XmlUtil();

    private XmlUtil() {
    }

    public static XmlUtil getInstance() {
        return instance;
    }

    public Document readXml(String filePath){
        // 获取流对象
        InputStream is = getInputStream(filePath);
        // 创建文档对象
        Document document = createDocument(is);
        return document;
    }


    private InputStream getInputStream(String filePath) {
        return this.getClass().getClassLoader().getResourceAsStream(filePath);
    }

    private Document createDocument(InputStream inputStream) {
        Document document = null;
        try {
            SAXReader reader = new SAXReader();
            document = reader.read(inputStream);
            return document;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }

}
